<template>
  <div class="index-container">
    <!--搜索-->
    <div class="search-box">
      <van-search v-model="searchVal" placeholder="请输入搜索关键字" disabled @click="$router.push('/searchPopup').catch(err => {})" />
    </div>
    <!--搜索弹出-->
    <transition name="van-slide-right">
      <router-view></router-view>
    </transition>
    <!--轮播图banner-->
    <div class="index-banner">
      <van-swipe :autoplay="3000" indicator-color="#b75d12">
        <van-swipe-item v-for="item in banner" :key="item.id">
          <van-image fit="cover" :src="item.image_url" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <!--频道-->
    <div class="index-channel">
      <van-grid :column-num="5">
        <van-grid-item v-for="item in channel" :key="item.id" @click="toList(item.id)">
          <van-image fit="cover" :src="item.icon_url" />
          <span class="text">{{item.name}}</span>
        </van-grid-item>
      </van-grid>
    </div>
    <!--品牌-->
    <div class="index-brand">
      <h2 class="title-h2">品牌制造商直供</h2>
      <ul>
        <li v-for="item in brand" :key="item.id" @click="toBrandList(item.id)"><div class="box">
          <van-image fit="cover" :src="item.list_pic_url" />
          <div class="tmain">
            <h2 class="tit">{{item.name}}</h2>
            <span class="price">{{item.floor_price | RMBformat}}</span>
          </div>
        </div></li>
      </ul>
    </div>
    <!--新品首发-->
    <div class="index-newGoods">
      <h2 class="title-h2">周一周四·新品首发</h2>
      <ul>
        <li v-for="item in newGoodsList" :key="item.id" @click="toProductsDetail(item.id)"><div class="box">
          <van-image fit="cover" :src="item.list_pic_url" />
          <h2 class="tit van-ellipsis">{{item.name}}</h2>
          <span class="price">{{item.retail_price | RMBformat}}</span>
        </div></li>
      </ul>
    </div>
    <!--人气推荐-->
    <div class="index-hotGoods">
      <h2 class="title-h2">人气推荐</h2>
      <ul>
        <li v-for="item in hotGoodsList" :key="item.id" @click="toProductsDetail(item.id)">
          <van-image fit="cover" :src="item.list_pic_url" />
          <div class="tmain">
            <h2 class="tit van-ellipsis">{{item.name}}</h2>
            <p class="summary">{{item.goods_brief}}</p>
            <span class="price">{{item.retail_price | RMBformat}}</span>
          </div>
        </li>
      </ul>
    </div>
    <!--专题精选-->
    <div class="index-special">
      <h2 class="title-h2">专题精选</h2>
      <div class="special-list">
        <van-swipe :loop="false" :width="300">
          <van-swipe-item v-for="item in topicList" :key="item.id">
            <van-image fit="cover" :src="item.item_pic_url" />
            <div class="tmain">
              <h2 class="tit van-ellipsis">{{item.title}}</h2>
              <p class="summary van-ellipsis">{{item.subtitle}}</p>
              <span class="price">{{item.price_info | RMBformat}}</span>
            </div>
          </van-swipe-item>
        </van-swipe>
      </div>
    </div>
    <!--分类板块-->
    <div class="index-category">
      <div class="list-item" v-for="list in categoryList" :key="list.id">
        <h2 class="title-h2">{{list.name}}</h2>
        <ul>
          <li v-for="item in list.goodsList" :key="item.id" @click="toProductsDetail(item.id)"><div class="box">
            <van-image fit="cover" :src="item.list_pic_url" />
            <h2 class="tit van-ellipsis">{{item.name}}</h2>
            <span class="price">{{item.retail_price | RMBformat}}</span>
          </div></li>
        </ul>
      </div>
    </div>
    <!--返回顶部-->
    <BackTop />
  </div>
</template>

<script>
import { GetHomeData } from '@/request/api'
import BackTop from '@/components/BackTop.vue'
export default {
  name: 'Home',
  components: {
    BackTop
  },
  data () {
    return {
      searchVal: '', // 搜索框内容
      banner: [], // 轮播图数据
      channel: [], // 频道数据
      brand: [], // 品牌数据
      newGoodsList: [], // 新品首发数据
      hotGoodsList: [], // 人气推荐
      topicList: [], // 专题精选
      categoryList: [] // 分类板块
    }
  },
  created () {
    GetHomeData().then(res => {
      // 请求成功之后执行
      // console.log('----then中的res是：', res)
      // 轮播图banner数据
      this.banner = res.data.banner
      // 频道数据
      this.channel = res.data.channel
      // 品牌数据
      this.brand = res.data.brandList
      // 新品首发数据
      this.newGoodsList = res.data.newGoodsList
      // 人气推荐
      this.hotGoodsList = res.data.hotGoodsList
      // 专题精选
      this.topicList = res.data.topicList
      // 分类板块
      this.categoryList = res.data.categoryList
    }).catch(err => {
      // 请求失败之后执行
      console.log(err)
    })
  },
  methods: {
    // 产品详情页跳转
    toProductsDetail (id) {
      this.$router.push('/productsDetail?id=' + id)
    },
    // 品牌详情跳转
    toBrandList (id) {
      this.$router.push('/brandDetail?id=' + id)
    },
    toList (id) {
      this.$router.push('/list?id=' + id)
    }
  }
}
</script>

<style  scoped>
.index-container {
  padding-top: 54px;
}

.index-container .title-h2 {
  font-size: 18px;
  color: #333;
  text-align: center;
  padding: 25px 0 20px 0;
}

.search-box {
  width: 100%;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 2;
}

.slide-enter,
.slide-leave-to {
  right: -100%;
}

.slide-enter-active,
.slide-leave-active {
  transition: right .3s;
}

.slide-enter-to,
.slide-leave {
  right: 0;
}

.index-banner {
  width: 100%;
}

.index-banner .van-image {
  width: 100%;
  height: 200px;
  display: block;
}

.index-banner .van-swipe__indicator {
  background-color: #fff;
  opacity: .8;
}

.index-channel {
  border-bottom: 10px solid #ebedf0;
}

.index-channel .van-grid-item__content::after {
  display: none;
}

.index-channel .van-image {
  width: 30px;
  height: 30px;
  display: block;
}

.index-channel .text {
  font-size: 14px;
  color: #333;
  margin-top: 5px;
}

.index-brand {
  padding-bottom: 5px;
  border-bottom: 10px solid #ebedf0;
}

.index-brand ul {
  display: flex;
  flex-wrap: wrap;
  padding: 0 5px;
}

.index-brand ul li {
  width: 50%;
  padding: 5px;
  box-sizing: border-box;
}

.index-brand ul li .box {
  width: 100%;
  position: relative;
}

.index-brand ul li .van-image {
  width: 100%;
  height: 102px;
  display: block;
}

.index-brand ul li .tmain {
  position: absolute;
  left: 10px;
  top: 15px;
}

.index-brand ul li .tit {
  font-size: 15px;
  color: #333;
  line-height: 1;
}

.index-brand ul li .price {
  font-size: 12px;
  color: #8b0000;
}

.index-newGoods {
  padding-bottom: 20px;
  border-bottom: 10px solid #ebedf0;
}

.index-newGoods ul {
  display: flex;
  flex-wrap: wrap;
  padding: 0 5px;
  margin-top: -10px;
}

.index-newGoods ul li {
  width: 50%;
  padding: 5px;
  box-sizing: border-box;
  text-align: center;
}

.index-newGoods ul li .box {
  width: 100%;
  position: relative;
}

.index-newGoods ul li .van-image {
  width: 100%;
  height: 172px;
  display: block;
}

.index-newGoods ul li .tit {
  font-size: 15px;
  color: #333;
  line-height: 1;
  margin-top: 5px;
}

.index-newGoods ul li .price {
  font-size: 12px;
  color: #8b0000;
}

.index-hotGoods {
  padding-bottom: 20px;
  border-bottom: 10px solid #ebedf0;
}

.index-hotGoods ul {
  padding: 0 10px;
  margin-top: -10px;
}

.index-hotGoods ul li {
  margin-top: 10px;
  background-color: #fafafa;
  display: flex;
}

.index-hotGoods ul li .van-image {
  width: 88px;
  height: 88px;
  display: block;
  flex-shrink: 0;
}

.index-hotGoods ul li .tmain {
  padding: 15px 10px;
  overflow: hidden;
}

.index-hotGoods ul li .tit {
  font-size: 16px;
  color: #333;
}

.index-hotGoods ul li .summary {
  font-size: 13px;
  color: #999;
  margin-top: 4px;
}

.index-hotGoods ul li .price {
  font-size: 12px;
  color: #8b0000;
  margin-top: 15px;
  display: block;
}

.index-special {
  padding-bottom: 20px;
  border-bottom: 10px solid #ebedf0;
}

.index-special .special-list {
  overflow: hidden;
}

.index-special .special-list .van-swipe {
  margin: 0 -10px;
}

.index-special .special-list .van-swipe-item {
  padding: 0 10px;
  box-sizing: border-box;
}

.index-special .special-list .van-swipe-item:last-child {
  margin-right: 0;
}

.index-special .special-list .van-swipe__indicators {
  display: none;
}

.index-special .special-list .van-image {
  width: 100%;
  height: 200px;
  display: block;
}

.index-special .special-list .tmain {
  padding: 10px 10px 0 10px;
  overflow: hidden;
  box-sizing: border-box;
}

.index-special .special-list .tit {
  font-size: 16px;
  color: #333;
}

.index-special .special-list .summary {
  font-size: 13px;
  color: #999;
  margin-top: 4px;
}

.index-special .special-list .price {
  font-size: 12px;
  color: #8b0000;
  margin-top: 10px;
  display: block;
}

.index-category {
  padding-bottom: 20px;
}

.index-category .title-h2::before {
  content: '';
  width: 30px;
  height: 1px;
  background-color: #ddd;
  display: inline-block;
  margin-right: 10px;
  vertical-align: middle;
}

.index-category .title-h2::after {
  content: '';
  width: 30px;
  height: 1px;
  background-color: #ddd;
  display: inline-block;
  margin-left: 10px;
  vertical-align: middle;
}

.index-category ul {
  display: flex;
  flex-wrap: wrap;
  padding: 0 5px;
  margin-top: -10px;
}

.index-category ul li {
  width: 50%;
  padding: 5px;
  box-sizing: border-box;
  text-align: center;
}

.index-category ul li .box {
  width: 100%;
  position: relative;
}

.index-category ul li .van-image {
  width: 100%;
  height: 172px;
  display: block;
}

.index-category ul li .tit {
  font-size: 15px;
  color: #333;
  line-height: 1;
  margin-top: 5px;
}

.index-category ul li .price {
  font-size: 12px;
  color: #8b0000;
}
</style>
